﻿using Application.AppService.EMRAppService;
using Application.AppService.SSAppServoice;
using Application.AppService.SSDAppServoice;
using AutoMapper;
using Domain;
using Domain.Entity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;

namespace UI.Controllers.Pay_Scale_Management_薪酬标准管理_
{
    public class SalaryReviewController : Controller
    {
        private readonly ISSAppService ssAppService; // 修正命名  
        private readonly IMapper mapper;
        private readonly IMemoryCache memCache;
        private readonly ILogger<SalaryReviewController> logger;

        public SalaryReviewController(ISSAppService ssAppService, IMapper mapper, IMemoryCache memCache, ILogger<SalaryReviewController> logger)
        {
            this.ssAppService = ssAppService;
            this.mapper = mapper;
            this.memCache = memCache;
            this.logger = logger;
        }

        /// <summary>
        /// 薪酬标准登记复核
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost("FenYeSS")]
        public IActionResult FenYeSS(string CheckStatus, int page, int pageSize)
        {
            string cacheId = "FenYeSS";
            SSFenYE sSFenYE = memCache.Get(cacheId) as SSFenYE;
            if (sSFenYE == null)
            {
                logger.LogInformation("从数据库获取");
                sSFenYE = ssAppService.FenYeSSAsync(CheckStatus, page, pageSize);

                //60秒后失效 
                memCache.Set(cacheId, sSFenYE, new TimeSpan(0, 0,60));
            }
            else
            {
                logger.LogInformation("从缓存获取");
            }
            return Json(sSFenYE);
        }
        public IActionResult Index1()
        {
            return View();
        }
        [HttpPost("UpdateSS")]
        public async Task<IActionResult> UpdateSS(SSReviewDto sSReviewDto)
        {
            SS ss = mapper.Map<SS>(sSReviewDto);
            bool rs = await ssAppService.UpdateSSAsync(ss);
            return Json(rs);
        }
        [HttpGet("GetSS")]
		public async Task<IActionResult> GetSS()
		{
            string cacheId = "GetSS" ;
            List<SS> sS = memCache.Get(cacheId) as List<SS>;
            if (sS == null)
            {
                sS = await ssAppService.SelectSSAsync();

                //5秒后失效
                memCache.Set(cacheId, sS, new TimeSpan(0, 0, 5));
            }
            else
            {
                logger.LogInformation("从缓存获取");
            }
            return Json(sS);
		}

		[HttpGet("SelectById/{id}")]
		public async Task<IActionResult> SelectById(int id)
		{
			string sS = await ssAppService.SelectById(id);
			return Json(sS);
		}

        [HttpGet("SelectSalarySum/{id}")]
		public async Task<IActionResult> SelectSalarySum(string id)
		{
            string cacheId = "SelectSalarySum" + id;
            SS ss = memCache.Get(cacheId) as SS;
            if (ss == null)
            {
                ss = await ssAppService.SelectSalarySum(id);

                //5秒后失效
                memCache.Set(cacheId, ss, new TimeSpan(0, 0, 5));
            }
            else
            {
                logger.LogInformation("从缓存获取");
            }
            return Json(ss);
		}

	}
}
